var prev_page = '<div class="mbtb-an prev">上一页</div>';
var next_page = '<div class="mbtb-an next">下一页</div>';
var page = getPage();
var center_page_count = 5;
// side_page_count 参数举例说明：[1][2][...][5][6][7][8][9][...][20][21]
var side_page_count = 2;// 当前页数相邻左右两边的分页按钮数量；[5][6]...[8][9]

function getPage() {
    var pathname = window.location.pathname.replace('.html', '');
    var pathname_arr = pathname.split('/');
    var page_index = pathname_arr.indexOf('page')
    if (page_index > -1) {
        return parseInt(pathname_arr[page_index + 1]);
    } else {
        return 1;
    }
}

function renderPage(all_pages) {
    if (window.location.pathname.indexOf('/page/') > -1) {
        var host = window.location.pathname.replace('.html', '').replace('/page/' + getPage(), '');
    } else {
        var host = window.location.pathname.replace('.html', '');
    }

    var page_html = '';
    if (all_pages > 1) {
        new Promise(resolve => {
            var show_page_arr = [];

            var init_min_page = center_page_count + side_page_count - 1;
            var init_max_page = all_pages - center_page_count;

            if (all_pages <= center_page_count + side_page_count * 2 + 1) {
                for (var j = 1; j <= all_pages; j++) {
                    show_page_arr.push(j);
                }
            } else {
                if (page < init_min_page) {
                    for (var j = 1; j < center_page_count + side_page_count * 2; j++) {
                        show_page_arr.push(j);
                    }
                    show_page_arr.push('...')
                    show_page_arr.push(all_pages - 1);
                    show_page_arr.push(all_pages);
                } else if (page >= init_min_page && page <= init_max_page) {
                    show_page_arr = [1, 2, '...'];
                    for (var j = 1; j <= center_page_count; j++) {
                        show_page_arr.push(page + j - (side_page_count + 1));
                    }
                    show_page_arr.push('...')
                    show_page_arr.push(all_pages - 1);
                    show_page_arr.push(all_pages);
                } else {
                    show_page_arr = [1, 2, '...'];
                    for (var j = all_pages - (center_page_count + side_page_count); j <= all_pages; j++) {
                        show_page_arr.push(j);
                    }
                }
            }
            resolve(show_page_arr)
        }).then(show_page_arr => {
            show_page_arr.forEach(val => {
                if (page == val) {
                    var active = 'active';
                    var url = 'javascript: void(0)';
                } else {
                    var active = '';
                    var url = val != '...' ? host + '/page/' + val + '.html' : 'javascript: void(0)';
                }

                page_html += '<a href="' + url + '" class="mbtb-an ' + active + '">' + val + '</a>';
            })

            var html = '<div class="mbt-btns">\n' +
                '                ' + (page > 1 ? prev_page : '') + '\n' +
                '                ' + page_html + '\n' +
                '                ' + (page < all_pages ? next_page : '') + '\n' +
                '            </div>';
            $('#page').html(html);
        })
    } else {
        $('#page').html('');
    }
}

// 上一页
$(document).on('click', '.prev', function (){
    if (window.location.pathname.indexOf('/page/') > -1) {
        var host = window.location.pathname.replace('.html', '').replace('/page/' + getPage(), '');
    } else {
        var host = window.location.pathname.replace('.html', '');
    }
    window.location.href = host + '/page/' + (getPage() - 1) + '.html';
})

// 下一页
$(document).on('click', '.next', function (){
    if (window.location.pathname.indexOf('/page/') > -1) {
        var host = window.location.pathname.replace('.html', '').replace('/page/' + getPage(), '');
    } else {
        var host = window.location.pathname.replace('.html', '');
    }
    window.location.href = host + '/page/' + (getPage() + 1) + '.html';
})